package com.crbim.cloud;

import com.crbim.cloud.common.encryption.genericencryption.*;
import com.crbim.cloud.common.encryption.jcsjptencryption.BasicDataAES;
import com.crbim.cloud.common.encryption.jcsjptencryption.BasicDataSM4;

import java.util.HashMap;

/**
 * 加密工具类使用实例（方便应用开发人员直接使用，无需二次研发）
 */
public class EncryptionDemo {

    public static void main(String[] args) {
        try {
            /**
             * AES-基础数据AES专用加密
             */
            String aesEn = BasicDataAES.encrypt("Password135!");
            System.out.println("AES-基础数据AES专用加密：" +aesEn);

            /**
             * AES-基础数据AES专用解密
             */
            String aesDe = BasicDataAES.decrypt(aesEn);
            System.out.println("AES-基础数据AES专用解密：" +aesDe);


            /**
             * SM4-基础数据SM4专用加密
             */
            String sm4En = BasicDataSM4.encryptData_SM4_CBC("Password135!");
            System.out.println("SM4-基础数据SM4专用加密：" +sm4En);

            /**
             * SM4-基础数据ASM4专用解密
             */
            String sm4De = BasicDataSM4.decryptData_SM4_CBC(sm4En);
            System.out.println("SM4-基础数据ASM4专用解密：" + sm4De);

            /**
             * AES-AES通用加密
             */
            String aesEnTy = GeneralAES.encrypt("Password135!","1231231231231234","1234567899874563");
            System.out.println("AES-AES通用加密：" +aesEnTy);

            /**
             * AES-AES通用解密
             */
            String aesDeTy = GeneralAES.decrypt(aesEnTy,"1231231231231234","1234567899874563");
            System.out.println("AES-AES通用解密：" +aesDeTy);


            /**
             * SM4-SM4通用加密
             */
            String sm4EnTy = GeneralSM4.encryptData_SM4_CBC("Password135!", "1231231231231234", "1234567899874563");
            System.out.println("SM4-SM4通用加密：" +sm4EnTy);

            /**
             * SM4-SM4通用解密
             */
            String sm4DeTy = GeneralSM4.decryptData_SM4_CBC(sm4EnTy,"1231231231231234","1234567899874563");
            System.out.println("SM4-SM4通用解密：" +sm4DeTy);

            /**
             * MD5-MD5加密
             */
            String md5En = GeneralMD5.encryptToMD5("Password135!");
            System.out.println("MD5-MD5加密：" +md5En);

            /**
             * Base64-Base64基本编码
             */
            String base64BasicEn = GeneralBase64.encoder("Password135 _+");
            System.out.println("Base64-Base64基本编码:" +base64BasicEn);

            /**
             * Base64-Base64基本解码
             */
            String base64BasicDe = GeneralBase64.decoder(base64BasicEn);
            System.out.println("Base64-Base64基本解码：" +base64BasicDe);


            /**
             * Base64-Base64-urlEncoder编码
             */
            String urlEncoder = GeneralBase64.urlEncoder("Password135 _+");
            System.out.println("Base64-Base64-urlEncoder编码:" +urlEncoder);

            /**
             * Base64-Base64-urlEncoder解码
             */
            String urlDecoder = GeneralBase64.urlDecoder(urlEncoder);
            System.out.println("Base64-Base64-urlEncoder解码：" +urlDecoder);


            /**
             * Base64-Base64-mimeEncoder编码
             */
            String mimeEncoder = GeneralBase64.mimeEncoder("Password135 _+");
            System.out.println("Base64-Base64-mimeEncoder编码:" +mimeEncoder);

            /**
             * Base64-Base64-mimeDecoder解码
             */
            String mimeDecoder = GeneralBase64.mimeDecoder(mimeEncoder);
            System.out.println("Base64-Base64-mimeDecoder解码：" +mimeDecoder);


            //RSA生成密钥对
            HashMap<String, String> keyPairMap = GeneralRSA.getKeyPairMap();
            String privateKey = keyPairMap.get("privateKey");
            String publicKey =  keyPairMap.get("publicKey");
            System.out.println("私钥 => " + privateKey + "\n");
            System.out.println("公钥 =>" + publicKey + "\n");

            // RSA加密
            String data = "123456111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111";
            String encryptData = GeneralRSA.encrypt(data, GeneralRSA.getPublicKey(publicKey));
            System.out.println("加密后内容 => " + encryptData + "\n");

            // RSA解密
            String decryptData = GeneralRSA.decrypt(encryptData, GeneralRSA.getPrivateKey(privateKey));
            System.out.println("data：" + data);
            System.out.println("解密后内容 => " + decryptData + "\n");


            // RSA签名
            String sign = GeneralRSA.sign(data, GeneralRSA.getPrivateKey(privateKey));

            // RSA验签
            boolean result = GeneralRSA.verify(data, GeneralRSA.getPublicKey(publicKey), sign);
            System.out.println("验签结果 => " + result + "\n");

        }catch (Exception E){
            E.printStackTrace();
        }
    }
}
